/**
 * @file getFlatSpec
 * @description getFlatSpec
 * @author fanxueliang<fanxueliang
 */

import {UnionSpecItem} from '@jsw/report-core';
import {each, omit, map} from 'lodash-es';
import {FlatSpecType} from '../interface';

export const getFlatSpecItem = (specItem: UnionSpecItem, prefix: string, modifySpec: any) => {
    let results: FlatSpecType[] = [];
    const {dataType} = specItem;
    const path = prefix || '';
    const modifySpecData = modifySpec[path]?.spec || {};
    switch (dataType) {
        case 'STRING':
        case 'COLOR':
        case 'BOOL':
        case 'NUMBER':
        case 'UPLOAD':
        case 'VIDEO':
        case 'AUDIO':
        case 'HTML':
        case 'URL':
        case 'ENUM':
            results.push({
                path,
                isShow: true,
                spec: {...specItem},
                modifySpec: modifySpecData
            });
            break;
        case 'OBJECT':
            if (specItem.items) {
                results.push({
                    path,
                    isShow: true,
                    spec: {
                        ...specItem,
                        items: []
                    },
                    modifySpec: modifySpecData
                });
                results = results.concat(getFlatSpec(specItem.items, modifySpec, path));
            } else {
                results.push({
                    path,
                    isShow: true,
                    spec: {...specItem},
                    modifySpec: modifySpecData
                });
            }
            break;
        case 'LIST':
            if (specItem.items) {
                results.push({
                    path,
                    isShow: true,
                    spec: {
                        ...specItem,
                        items: []
                    },
                    modifySpec: modifySpecData
                });
                results = results.concat(getFlatSpec(specItem.items, modifySpec, `${path}[*]`));
            } else if (specItem.element) {
                results.push({
                    path,
                    isShow: true,
                    spec: omit(specItem, 'element'),
                    modifySpec: modifySpecData
                });
                results = results.concat(getFlatSpecItem(specItem.element, `${path}[*]`, modifySpec));
            }
            break;
        case 'ALTERNATIVE':
            const enumValues = specItem.enumValues;
            const resetEnumValues = map(enumValues, (enumItem) => ({
                ...enumItem,
                items: []
            }));
            results.push({
                path,
                isShow: true,
                spec: {
                    ...omit(specItem, 'enumValues'),
                    enumValues: resetEnumValues
                },
                modifySpec: modifySpecData
            });
            each(enumValues, ({value, items}) => {
                if (items) {
                    results = results.concat(getFlatSpec(items, modifySpec, `${path}.${value}`));
                }
            });
            break;
        // case 'ACTIONLINK':
        //     results.push({
        //         path,
        //         isShow: true,
        //         spec: {
        //             ...specItem
        //         },
        //         modifySpec: modifySpecData
        //     });
        //     break;
        default:
            throw new Error(`dataType ${dataType} is unknown!`);
    }
    return results;
};

export const getFlatSpec = (spec: UnionSpecItem[], modifySpec: any, prefix: string) => {
    let result: FlatSpecType[] = [];
    if (spec?.length) {
        each(spec, (item) => {
            const {name} = item;
            const path = prefix ? `${prefix}.${name}` : `${name}`;
            result = result.concat(getFlatSpecItem(item, path, modifySpec));
        });
    }
    return result;
};
